package com.gitee.wsl.transform.converter.case


import com.gitee.wsl.transform.converter.AbstractConverter
import com.gitee.wsl.transform.converter.Converter
import com.gitee.wsl.transform.converter.StringConverter
import com.gitee.wsl.transform.converter.convert


private class StringCaseConverter(
    private val sourceFormat: CaseFormat,
    private val targetFormat: CaseFormat
) : AbstractConverter<String, String>() {

    override fun doForward(a: String): String {

        return sourceFormat.converterTo(targetFormat).convert(a)
    }

    override fun doBackward(b: String): String {
        return targetFormat.converterTo(sourceFormat).convert(b)
    }

    override fun equals(`object`: Any?): Boolean {
        if (`object` is StringCaseConverter) {
            val that = `object`
            return sourceFormat == that.sourceFormat && targetFormat == that.targetFormat
        }
        return false
    }

    override fun hashCode(): Int {
        return sourceFormat.hashCode() xor targetFormat.hashCode()
    }

    override fun toString(): String {
        return "$sourceFormat.converterTo($targetFormat)"
    }
}

/**
 * Returns a serializable `Converter` that converts strings from this format to `targetFormat`.
 *
 * @since 16.0
 * */

fun CaseFormat.converterTo(targetFormat: CaseFormat): AbstractConverter<String, String> {
    return StringCaseConverter(this, targetFormat)
}
